package com.gqb.servlet;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.gqb.load.ControllerLoad;
import com.gqb.util.ClassUtil;
import com.gqb.web.Handler;
import com.gqb.web.ModelAndView;

public class DispatcherServlet extends HttpServlet {
	
	

	private static final long serialVersionUID = -4581069463408626108L;
	
	
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext servletContext=config.getServletContext();
		//用来注册对jsp请求的处理，配置jsp放置的路径，获取u
		ServletRegistration jspServlet=servletContext.getServletRegistration("jsp");
		jspServlet.addMapping("/WEB-INF/view/*");
		
	
	}



	/**
	 * 重写service 解析请求，封装参数，获得结果返回
	 */
	@Override
	protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
			throws ServletException, IOException {
		String methodType=httpServletRequest.getMethod();
		//获取应用路径名下的路劲 http://localhost:8080/WriteFramework/index中的"/index"部分
		String requestUrl=httpServletRequest.getPathInfo();
		System.out.println(httpServletRequest.toString());
		System.out.println(methodType+requestUrl);
		Handler handler=ControllerLoad.getHandler(methodType, requestUrl);
		if(handler!=null){
			Class<?> controllerClass=handler.getControllerClass();
			Object controllerObj=ControllerLoad.getController(controllerClass);
			try {
				Object result=ClassUtil.invokeMethod(controllerObj, handler.getMethod());
				if(result instanceof ModelAndView){
					ModelAndView modeView=(ModelAndView) result;
					Map<String, Object> paramMap=modeView.getModel();
					Iterator<String> iterator=paramMap.keySet().iterator();
					while(iterator.hasNext()){
						String key=iterator.next();
						httpServletRequest.setAttribute(key, paramMap.get(key));
					}
					//forward to a view
					String dispatherUrl=modeView.getViewPath();
					if(!StringUtils.isEmpty(dispatherUrl)){
						if(dispatherUrl.startsWith("/")){
							System.out.println(httpServletRequest.getContextPath());
							httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+dispatherUrl);
						}else{
							RequestDispatcher rd=httpServletRequest.getRequestDispatcher("/WEB-INF/view/"+dispatherUrl);
							rd.forward(httpServletRequest, httpServletResponse);
						}
					}
//					httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+dispatherUrl);
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}
